package com.bq.gametrade.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.DialogPreference;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bq.gametrade.R;
import com.bq.gametrade.utils.SharedPreferencesUtils;

public class LocationActivity extends AppCompatActivity implements View.OnClickListener{

    private LocationClient locationClient = null;
    private Button btnGetAddress;
    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        tvShow = (TextView)findViewById(R.id.tv_shows);

        btnGetAddress= (Button)findViewById(R.id.btn_getAddress);
        btnGetAddress.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(locationClient !=null){
            locationClient.requestLocation();
            locationClient.start();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isGetAddress = (boolean)SharedPreferencesUtils.getValue(LocationActivity.this,SharedPreferencesUtils.get_address,false);
        if(!isGetAddress){
            initGps();
        }

    }

    private void initGps() {
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        if (!locationManager
                .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            Toast.makeText(LocationActivity.this, "请打开GPS",
                    Toast.LENGTH_SHORT).show();
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("比奇交易需要临时获取您的位置,请打开GPS");
            dialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {

                            // 转到手机设置界面，用户设置GPS
                            Intent intent = new Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivityForResult(intent, 0); // 设置完成后返回到原来的界面

                        }
                    });
            dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    arg0.dismiss();
                }
            } );
            dialog.show();
        } else {
            // 弹出Toast
//          Toast.makeText(TrainDetailsActivity.this, "GPS is ready",
//                  Toast.LENGTH_LONG).show();
//          // 弹出对话框
//          new AlertDialog.Builder(this).setMessage("GPS is ready")
//                  .setPositiveButton("OK", null).show();
            try{
                MyLocationListenner myListener = new MyLocationListenner();
                locationClient = new LocationClient(LocationActivity.this);
                locationClient.registerLocationListener(myListener);
                LocationClientOption option = new LocationClientOption();
                option.setOpenGps(true);
//            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
                option.setCoorType("bd09ll");
                option.setScanSpan(5000);
                option.disableCache(true);
                option.setIsNeedAddress(true);

                option.setPriority(LocationClientOption.NetWorkFirst);
                locationClient.setLocOption(option);
                locationClient.start();
                SharedPreferencesUtils.saveValue(LocationActivity.this,SharedPreferencesUtils.get_address,true);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    class MyLocationListenner implements BDLocationListener{
        @Override
        public void onReceiveLocation(BDLocation location) {

            if (location == null)
                return;
            Log.e("wu",""+location.getAddrStr()+"\n"+location.getProvince()+location.getCity()+" ");
            StringBuffer sb = new StringBuffer(256);
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
//				sb.append(location.getAddrStr());
                sb.append(location.getCity());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append(location.getCity());
            }
            if (sb.toString() != null && sb.toString().length() > 0) {


            }

        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }




    @Override
    protected void onStop() {
        super.onStop();
        if(locationClient != null){
            locationClient.stop();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
